package org.example.ini;

import org.example.constant.OcrItem;
import org.ini4j.Ini;
import org.ini4j.Profile;

import java.io.File;
import java.lang.reflect.Field;

public class IniReader {


    public static <T> T read(String path, OcrItem selection, Class<T> clazz) {
        try {
            File file = new File(path);
            if (!file.exists() || file.isDirectory()) {
                throw new RuntimeException("配置文件不存在");
            }
            Ini ini = new Ini();
            ini.load(file);

            T instance = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();

            Profile.Section section = ini.get(selection.getType());
            for (Field field : fields) {
                field.setAccessible(true);
                field.set(instance, section.get(field.getName()));
            }
            return instance;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
